package org.xenon.vote.dao.imp;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.xenon.vote.dao.StringDao;
import org.xenon.vote.model.pojo.po.Rec;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class RecDao implements StringDao<Rec> {
    private final JdbcTemplate jdbcTemplate;

    public RecDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public void add(Rec rec) {
        String sql = "insert into `vote`.`rec` (`token`, `mail`, `passwd`, `birth`, `age`) values (?, ?, ?, ?, ?)";
        jdbcTemplate.update(sql, rec.getToken(), rec.getMail(), rec.getPasswd(), rec.getBirth(), rec.getAge());
    }

    @Override
    @Deprecated
    public void edit(Rec rec) {
        String sql = "update `vote`.`rec` set `mail` = ? , `passwd` = ? , `birth` = ?, `age` = ?where `token` = ?";
        jdbcTemplate.update(sql, rec.getMail(), rec.getPasswd(), rec.getToken(), rec.getBirth(), rec.getAge());
    }

    @Override
    public Rec get(String token){
        String sql = "select * from `vote`.`rec` where `token` = ?";
        List<Rec> list = jdbcTemplate.query(sql, new Object[]{String.valueOf(token)}, RecRowMapper.getInstance());
        if (list.size() == 0) return null;
        return list.get(0);
    }

    @Deprecated
    @Override
    public List<Rec> getAll(String id) {
        return null;
    }

    public List<Rec> getAll() {
        String sql = "select * from `vote`.`rec` where true";
        return jdbcTemplate.query(sql, RecRowMapper.getInstance());
    }

    @Override
    public void del(Rec rec) {
        String sql = "delete FROM `vote`.`rec` WHERE `token` = ?";
        jdbcTemplate.update(sql, rec.getToken());
    }

    @Override
    public void del(String token){
        String sql = "delete FROM `vote`.`rec` WHERE `token` = ?";
        jdbcTemplate.update(sql, token);
    }

    public void delAll(String mail){
        String sql = "delete FROM `vote`.`rec` WHERE `mail` = ?";
        jdbcTemplate.update(sql, mail);
    }

    /**
     * 自动以RowMapper
     * 提高性能, 使用单例模式
     */
    private static class RecRowMapper implements RowMapper<Rec> {

        private static RecRowMapper recRowMapper;
        @Override
        public Rec mapRow(ResultSet rs, int i) throws SQLException {
            return new Rec(rs.getString("token"),
                    rs.getString("mail"),
                    rs.getString("passwd"),
                    rs.getDate("birth"),
                    rs.getInt("age"));
        }
        static RecRowMapper getInstance(){
            if (recRowMapper == null){
                synchronized (RecRowMapper.class){
                    if(recRowMapper ==null){
                        recRowMapper = new RecRowMapper();
                    }
                }
            }
            return recRowMapper;
        }
    }

}
